package www.openteam.com.ui.policy.other;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.cundong.recyclerview.HeaderAndFooterRecyclerViewAdapter;

import www.openteam.com.R;
import www.openteam.com.adapter.policy.other.CODetailedAdapter;
import www.openteam.com.adapter.policy.other.PODetailedAdapter;
import www.openteam.com.bean.OrderApproval;
import www.openteam.com.bean.ProOrder;
import www.openteam.com.tool.RecyclerViewStateUtils;
import www.openteam.com.widget.LoadingFooter;

/**
 * Created by kylin on 2017/4/6.
 */

public class PODetailedFragement extends Fragment {

    private static final String TAG = PODetailedFragement.class.getSimpleName();
    private LinearLayoutManager linearLayoutManager;
    private RecyclerView mRecyclerView;
    private PODetailedAdapter poDetailedAdapter;
    private ProOrder.DataEntity orderApprovalitems;
    private HeaderAndFooterRecyclerViewAdapter mFooterRecyclerViewAapter;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        orderApprovalitems = bundle.getParcelable("queryCustom");
    }


    @Override
    public View onCreateView(LayoutInflater inflater,  ViewGroup container,  Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_codetailed, container, false)  ;
        initRView(view);
        setPbProductInfosAdapter();
        return view;
    }

    public void initRView(View view) {

        mRecyclerView = (RecyclerView) view.findViewById(R.id.recyclerview_search);

        linearLayoutManager = new LinearLayoutManager(getActivity());
        mRecyclerView.setLayoutManager(linearLayoutManager);
    }

    private void setPbProductInfosAdapter() {
        if (poDetailedAdapter == null) {
            poDetailedAdapter = new PODetailedAdapter(getContext());
            poDetailedAdapter.addItems(orderApprovalitems);
            poDetailedAdapter.addContext(getActivity());
            mFooterRecyclerViewAapter = new HeaderAndFooterRecyclerViewAdapter(poDetailedAdapter);
            mRecyclerView.setAdapter(mFooterRecyclerViewAapter);
            RecyclerViewStateUtils.setFooterViewState(this.mRecyclerView, LoadingFooter.State.Normal);
        } else {
            mFooterRecyclerViewAapter.notifyDataSetChanged();
            poDetailedAdapter.addItems(orderApprovalitems);
            poDetailedAdapter.addContext(getActivity());
            RecyclerViewStateUtils.setFooterViewState(this.mRecyclerView, LoadingFooter.State.Normal);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        poDetailedAdapter =null;
    }
}
